package com.chengang.crawler.jd.controller;

import com.chengang.crawler.jd.model.Tuple;
import com.chengang.crawler.jd.util.HttpUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 陈刚
 * @ClassName XiaoshuoController
 * @Description //TODO
 * @create 2020-02-09 11:10
 */
@RestController()
public class XiaoshuoController {

    @Autowired
    HttpUtils httpUtils;

    @RequestMapping("/download")
    public String download(@RequestParam(name = "url") String url) throws IOException {
        System.out.println(url);
        String urlPrefix = url.substring(0, url.lastIndexOf("/"));
        Tuple<String, List<String>> index = parseIndex(url);
        String title = index.getA();
        String content = index.getB().stream()
                .map(href -> downloadone(urlPrefix+"/"+href))
                .collect(Collectors.joining());
        String data = title+"\n"+content;
        FileUtils.write(new File("D:/小说/"+title+".txt"),data,"utf8");
        return "下载成功\n地址：D:/小说/"+title+".txt";
    }

    /**
     * 下载其中一章的数据
     * @param url
     * @return
     */
    private Tuple<String, List<String>> parseIndex(String url) {
        String html = httpUtils.doGetHtml(url);
        Document document = Jsoup.parse(html);
        String title = document.getElementsByTag("title").text();
        Elements elements = document.select(".panel-chapterlist a");
        List<String> hrefs = elements.stream().map(element -> element.attr("href"))
                .collect(Collectors.toList());
        return new Tuple<String,List<String>>(title,hrefs);
    }

    /**
     * 下载其中一章的数据
     * @param url
     * @return
     */
    private String downloadone(String url) {
        String html = httpUtils.doGetHtml(url);
        if (StringUtils.isEmpty(html)) {
            return "";
        }
        Document document = Jsoup.parse(html);
        Element element = document.getElementById("htmlContent");
        String content = element.html();
        content = content.replace("&nbsp;","")
                .replaceAll("<br>\\s<br>","");
//        String title = document.select(".breadcrumb li:eq(1)").text();
        String readTitle = document.getElementsByClass("readTitle").text();
//        System.out.println("title = " + title);
        return "章节："+readTitle+"\n"+content;
    }
}
